package jq.dao;

import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

public class IEntityManagerImpl implements IEntityManager {
	private PersistenceManager pm = PMF.get().getPersistenceManager();
	private static volatile IEntityManagerImpl impl = new IEntityManagerImpl();

	private IEntityManagerImpl() {
	}

	public static IEntityManagerImpl getInstance() {
		if (impl == null) {
			synchronized (IEntityManagerImpl.class) {
				if (impl == null) {
					impl = new IEntityManagerImpl();
				}
			}
		}
		return impl;
	}

	@Override
	public void saveEntity(Object obj) {
		try {
			pm.makePersistent(obj);
		} finally {
			 
		}
	}

	@Override
	public void saveEntityList(Collection<?> objs) {
		try {
			pm.makePersistentAll(objs);
		} finally {
			 
		}
	}

	@Override
	public List<?> queryAllEntity(Class<?> c) {
		try{
			Query q=pm.newQuery(c);
			List<?> result=(List<?>) q.execute();
			return (List<?>) pm.detachCopyAll(result);
		}finally{
			 
		}
	}

	@Override
	public int queryEntityCount(Class<?> c) {
		try{
			Query q=pm.newQuery(c);
			q.setResult("count(this)");
			int count=0;
			count=(Integer) q.execute();
			return count;
		}finally{
		}
	}

}
